package managesystem.poverty;

import managesystem.JDBCUtils;
import managesystem.help.*;
import managesystem.item.Item;
import managesystem.login.Login;

import javax.swing.*;
import javax.swing.table.*;
import java.awt.*;
import java.awt.event.*;
import java.util.ArrayList;

/**
 * @author The setting sun
 * @version 1.0
 * @title: PovertyShowAllInfo
 * @description: TODO
 * @date 2021/12/4 10:28
 */
public class PovertyUI {
    JFrame frame = new JFrame("贫困用户");

    JMenuBar menu = new JMenuBar();

    JMenu user = new JMenu("用户");
    JMenuItem userRegisterInfo = new JMenuItem("登记信息");
    JMenuItem bindUser = new JMenuItem("用户绑定");
    JMenuItem logoutInfo = new JMenuItem("注销信息");
    JMenuItem returnLogin = new JMenuItem("返回登录界面");

    JMenu ope = new JMenu("功能");
    JMenu show = new JMenu("查询");
    JMenuItem lookMe = new JMenuItem("个人信息");
    JMenuItem showMyHelp = new JMenuItem("我的帮扶人员");
    JMenuItem showMyItem = new JMenuItem("我的贫困项目");
    JMenuItem showOnePoverty = new JMenuItem("贫困人员");
    JMenuItem showOneItem = new JMenuItem("贫困项目");
    JMenu update = new JMenu("修改");
    JMenuItem updateUserInfo = new JMenuItem("个人信息");

    JTabbedPane tabbed = new JTabbedPane();

    DefaultTableModel dtmPoverty = new DefaultTableModel();
    JTable povertyTable = new JTable(dtmPoverty);
    JScrollPane povertyScroll = new JScrollPane(povertyTable);

    DefaultTableModel dtmItem = new DefaultTableModel();
    JTable itemTable = new JTable(dtmItem);
    JScrollPane itemScroll = new JScrollPane(itemTable);

    DefaultTableModel dtm = new DefaultTableModel();
    JTable table = new JTable(dtm);
    JScrollPane scroll = new JScrollPane(table);
    JTableHeader head = table.getTableHeader();

    Integer povertyId;

//    public static void main(String[] args) {
//        new PovertyUI().init();
//    }

    public void init() {
        showPovertyInfo();
        showItemInfo();
        listen();

        logoutInfo.setVisible(false);
        lookMe.setVisible(false);
        showMyHelp.setVisible(false);
        showMyItem.setVisible(false);
        update.setVisible(false);

        user.setFont(new Font("微软雅黑", Font.BOLD, 18));
        userRegisterInfo.setFont(new Font("微软雅黑", Font.BOLD, 14));
        bindUser.setFont(new Font("微软雅黑", Font.BOLD, 14));
        logoutInfo.setFont(new Font("微软雅黑", Font.BOLD, 14));
        returnLogin.setFont(new Font("微软雅黑", Font.BOLD, 14));
        user.setForeground(new Color(0xE74C0B));
        userRegisterInfo.setForeground(new Color(0xE74C0B));
        bindUser.setForeground(new Color(0xE74C0B));
        logoutInfo.setForeground(new Color(0xE74C0B));
        returnLogin.setForeground(new Color(0xE74C0B));

        ope.setFont(new Font("微软雅黑", Font.BOLD, 18));
        ope.setForeground(new Color(0xE74C0B));

        show.setFont(new Font("微软雅黑", Font.BOLD, 15));
        lookMe.setFont(new Font("微软雅黑", Font.BOLD, 14));
        showMyHelp.setFont(new Font("微软雅黑", Font.BOLD, 14));
        showMyItem.setFont(new Font("微软雅黑", Font.BOLD, 14));
        showOnePoverty.setFont(new Font("微软雅黑", Font.BOLD, 14));
        showOneItem.setFont(new Font("微软雅黑", Font.BOLD, 14));
        show.setForeground(new Color(0xE74C0B));
        lookMe.setForeground(new Color(0xE74C0B));
        showMyHelp.setForeground(new Color(0xE74C0B));
        showMyItem.setForeground(new Color(0xE74C0B));
        showOnePoverty.setForeground(new Color(0xE74C0B));
        showOneItem.setForeground(new Color(0xE74C0B));

        update.setFont(new Font("微软雅黑", Font.BOLD, 15));
        updateUserInfo.setFont(new Font("微软雅黑", Font.BOLD, 14));
        update.setForeground(new Color(0xE74C0B));
        updateUserInfo.setForeground(new Color(0xE74C0B));

        JTableHeader povertyHead = povertyTable.getTableHeader();
        povertyHead.setFont(new Font("微软雅黑", 1, 15));
        povertyHead.setReorderingAllowed(false);

        povertyTable.setFont(new Font("微软雅黑", 1, 13));
        povertyTable.setEnabled(false);
        povertyTable.setRowHeight(20);

        JTableHeader itemHead = itemTable.getTableHeader();
        itemHead.setFont(new Font("微软雅黑", 1, 15));
        itemHead.setReorderingAllowed(false);

        itemTable.setFont(new Font("微软雅黑", 1, 13));
        itemTable.setEnabled(false);
        itemTable.setRowHeight(20);

        user.add(userRegisterInfo);
        user.add(bindUser);
        user.add(logoutInfo);
        user.add(returnLogin);

        ope.add(show);

        show.add(lookMe);
        show.add(showMyHelp);
        show.add(showMyItem);
        show.add(showOnePoverty);
        show.add(showOneItem);

        ope.add(update);
        update.add(updateUserInfo);

        menu.add(user);
        menu.add(ope);

        tabbed.add("贫困人员信息", povertyScroll);
        tabbed.add("贫困项目信息", itemScroll);

        frame.setJMenuBar(menu);
        frame.add(tabbed);

        frame.setVisible(true);
        frame.setSize(1200, 600);
        frame.setLocationRelativeTo(null);
        frame.setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
    }

    public void listen() {
        userRegisterInfo.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new PovertyJoinItem().init();
            }
        });

        bindUser.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new BindUserDialog().init();
            }
        });

        logoutInfo.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new logoutInfoDialog().init();
            }
        });

        returnLogin.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                frame.dispose();
                new Login().init();
            }
        });

        lookMe.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String lookMe = "select * from user_poor where pid = ?";
                Poverty poverty = JDBCUtils.getPovertyOneInfo(lookMe, povertyId);

                PublicTableShowInfo dialog = new PublicTableShowInfo();
                dialog.setSize(1200, 150);
                dialog.init();

                dtm.getDataVector().removeAllElements();

                String[] title = new String[]{"贫困人员编号", "帮扶人员编号", "项目编号", "姓名", "年龄", "性别", "民族", "籍贯", "贫困原因", "身体状况", "受教育程度", "登记时间"};
                dtm.setColumnIdentifiers(title);

                dtm.addRow(new Object[]{poverty.getPid(), poverty.getHid(), poverty.getIid(), poverty.getPname(),
                        poverty.getPage(), poverty.getPsex(), poverty.getPnation(), poverty.getBirthplace(),
                        poverty.getReason(), poverty.getHealth(), poverty.getEducated(), poverty.getDate()});
            }
        });

        showMyHelp.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String getMe = "select * from user_poor where pid = ?";
                Poverty poverty = JDBCUtils.getPovertyOneInfo(getMe, povertyId);

                if (poverty.getHid() != null) {
                    String showMyHelp = "select h.* from user_poor p join user_helper h on p.hid = h.hid where p.pid = ?";
                    Help help = JDBCUtils.getHelpOneInfo(showMyHelp, povertyId);

                    PublicTableShowInfo dialog = new PublicTableShowInfo();
                    dialog.setSize(600, 200);
                    dialog.init();

                    dtm.getDataVector().removeAllElements();

                    String[] title = new String[]{"帮扶人员编号", "姓名", "年龄", "性别", "民族", "地址"};
                    dtm.setColumnIdentifiers(title);

                    dtm.addRow(new Object[]{help.getHid(), help.getHname(), help.getHage(),
                            help.getHsex(), help.getHnation(), help.getAddress()});
                } else {
                    new PovertyHidIsNULLHint().init();
                }
            }
        });

        showMyItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                String getMe = "select * from user_poor where pid = ?";
                Poverty poverty = JDBCUtils.getPovertyOneInfo(getMe, povertyId);

                if (poverty.getIid() != null) {
                    String showMyItem = "select i.* from user_poor p join items i on p.iid = i.iid where p.pid = ?";
                    Item item = JDBCUtils.getItemOneInfo(showMyItem, povertyId);

                    PublicTableShowInfo dialog = new PublicTableShowInfo();
                    dialog.setSize(500, 150);
                    dialog.init();

                    dtm.getDataVector().removeAllElements();

                    String[] title = new String[]{"项目编号", "项目名称", "项目类型"};
                    dtm.setColumnIdentifiers(title);

                    dtm.addRow(new Object[]{item.getIid(), item.getIname(), item.getIclass()});
                } else {
                    new PovertyIidIsNULLHint().init();
                }
            }
        });

        showOnePoverty.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new ShowOnePovertyDialog().init();
            }
        });

        showOneItem.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new ShowOneItemDialog().init();
            }
        });

        updateUserInfo.addActionListener(new ActionListener() {
            @Override
            public void actionPerformed(ActionEvent e) {
                new UpdateUserInfoDialog().init();
            }
        });
    }

    public void showPovertyInfo() {
        String sql = "select * from user_poor";
        ArrayList<Poverty> list = JDBCUtils.getPovertyAllInfo(sql);

        String[] title = new String[]{"贫困人员编号", "帮扶人员编号", "项目编号", "姓名", "年龄", "性别", "民族", "籍贯", "贫困原因", "身体状况", "受教育程度", "登记时间"};
        dtmPoverty.setColumnIdentifiers(title);

        for (int i = 0; i < list.size(); i++) {
            dtmPoverty.addRow(new Object[]{list.get(i).getPid(), list.get(i).getHid(), list.get(i).getIid(), list.get(i).getPname(),
                    list.get(i).getPage(), list.get(i).getPsex(), list.get(i).getPnation(), list.get(i).getBirthplace(),
                    list.get(i).getReason(), list.get(i).getHealth(), list.get(i).getEducated(), list.get(i).getDate()});
        }
    }

    public void showItemInfo() {
        String sql = "select * from items";
        ArrayList<Item> list = JDBCUtils.getItemAllInfo(sql);

        String[] title = new String[]{"项目编号", "项目名称", "项目类型"};
        dtmItem.setColumnIdentifiers(title);

        for (int i = 0; i < list.size(); i++) {
            dtmItem.addRow(new Object[]{list.get(i).getIid(), list.get(i).getIname(), list.get(i).getIclass()});
        }
    }

    class BindUserDialog extends JDialog {
        JLabel pidLabel = new JLabel("请输入你的贫困人员编号：");
        JTextField pidText = new JTextField(10);
        JButton pidButton = new JButton("确定");

        public void init() {
            listen();

            pidLabel.setFont(new Font("微软雅黑", 1, 16));
            pidText.setFont(new Font("微软雅黑", 1, 14));
            pidButton.setFont(new Font("微软雅黑", 1, 14));

            setLayout(new FlowLayout());
            add(pidLabel);
            add(pidText);
            add(pidButton);

            setVisible(true);
            setSize(500, 100);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        }

        public void listen() {
            String getPoverty = "select * from user_poor";
            ArrayList<Poverty> povertyList = JDBCUtils.getPovertyAllInfo(getPoverty);

            pidButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String getMe = "select * from user_poor where pid = ?";
                    Poverty poverty = JDBCUtils.getPovertyOneInfo(getMe, pidText.getText());

                    if (poverty.getHid() == null && poverty.getIid() == null) {
                        new PovertyHidIidIsNULLHint().init();
                    } else if (poverty.getHid() == null) {
                        new PovertyHidIsNULLHint().init();
                    } else if (poverty.getIid() == null) {
                        new PovertyIidIsNULLHint().init();
                    }

                    boolean povertyIs = false;
                    for (int i = 0; i < povertyList.size(); i++) {
                        if (String.valueOf(povertyList.get(i).getPid()).equals(pidText.getText())) {
                            povertyIs = true;
                        }
                    }

                    if (povertyIs == false) {
                        new BindFailureHint().init();
                    } else {
                        povertyId = new Integer(pidText.getText());

                        userRegisterInfo.setVisible(false);
                        bindUser.setVisible(false);
                        logoutInfo.setVisible(true);
                        lookMe.setVisible(true);
                        showMyHelp.setVisible(true);
                        showMyItem.setVisible(true);
                        update.setVisible(true);

                        dispose();
                    }

                }
            });
        }
    }

    class logoutInfoDialog extends JDialog {
        JLabel confirmLabel = new JLabel("你确定要注销你的信息吗？");
        JButton confirm = new JButton("确定");
        JButton cancel = new JButton("取消");

        public void init() {
            listen();

            confirmLabel.setFont(new Font("微软雅黑", 1, 20));
            confirm.setFont(new Font("微软雅黑", 1, 14));
            cancel.setFont(new Font("微软雅黑", 1, 14));

            confirmLabel.setForeground(Color.RED);

            setLayout(new FlowLayout());
            add(confirmLabel);
            add(confirm);
            add(cancel);

            setVisible(true);
            setSize(300, 140);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        }

        public void listen() {
            confirm.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    dispose();

                    String delete = "delete from user_poor where pid = ?";
                    JDBCUtils.deletePoverty(delete, povertyId);

                    dtmPoverty.getDataVector().removeAllElements();

                    String sql = "select * from user_poor";
                    ArrayList<Poverty> list = JDBCUtils.getPovertyAllInfo(sql);

                    String[] title = new String[]{"贫困人员编号", "帮扶人员编号", "项目编号", "姓名", "年龄", "性别", "民族", "籍贯", "贫困原因", "身体状况", "受教育程度", "登记时间"};
                    dtmPoverty.setColumnIdentifiers(title);

                    for (int i = 0; i < list.size(); i++) {
                        dtmPoverty.addRow(new Object[]{list.get(i).getPid(), list.get(i).getHid(), list.get(i).getIid(), list.get(i).getPname(),
                                list.get(i).getPage(), list.get(i).getPsex(), list.get(i).getPnation(), list.get(i).getBirthplace(),
                                list.get(i).getReason(), list.get(i).getHealth(), list.get(i).getEducated(), list.get(i).getDate()});
                    }
                }
            });

            cancel.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    dispose();
                }
            });
        }
    }

    class ShowOnePovertyDialog extends JDialog {
        JLabel pidLabel = new JLabel("请输入你要查询的贫困人员编号：");
        JTextField pidText = new JTextField(10);
        JButton pidButton = new JButton("确定");

        public void init() {
            listen();

            pidLabel.setFont(new Font("微软雅黑", 1, 16));
            pidText.setFont(new Font("微软雅黑", 1, 14));
            pidButton.setFont(new Font("微软雅黑", 1, 14));

            setLayout(new FlowLayout());
            add(pidLabel);
            add(pidText);
            add(pidButton);

            setVisible(true);
            setSize(500, 100);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        }

        public void listen() {
            String getPoverty = "select * from user_poor";
            ArrayList<Poverty> povertyList = JDBCUtils.getPovertyAllInfo(getPoverty);

            pidButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    boolean povertyIs = false;
                    for (int i = 0; i < povertyList.size(); i++) {
                        if (String.valueOf(povertyList.get(i).getPid()).equals(pidText.getText())) {
                            povertyIs = true;
                        }
                    }

                    if (povertyIs == false) {
                        new ShowOnePovertyFailureHint().init();
                    } else {
                        dispose();

                        PublicTableShowInfo dialog = new PublicTableShowInfo();
                        dialog.setSize(1200, 150);
                        dialog.init();

                        dtm.getDataVector().removeAllElements();

                        Integer pid = new Integer(pidText.getText());
                        String sql = "select * from user_poor where pid = ?";
                        Poverty poverty = JDBCUtils.getPovertyOneInfo(sql, pid);

                        String[] title = new String[]{"贫困人员编号", "帮扶人员编号", "项目编号", "姓名", "年龄", "性别", "民族", "籍贯", "贫困原因", "身体状况", "受教育程度", "登记时间"};
                        dtm.setColumnIdentifiers(title);

                        dtm.addRow(new Object[]{poverty.getPid(), poverty.getHid(), poverty.getIid(), poverty.getPname(),
                                poverty.getPage(), poverty.getPsex(), poverty.getPnation(), poverty.getBirthplace(),
                                poverty.getReason(), poverty.getHealth(), poverty.getEducated(), poverty.getDate()});
                    }
                }
            });
        }
    }

    class ShowOneItemDialog extends JDialog {
        JLabel iidLabel = new JLabel("请输入你要查询的贫困项目编号：");
        JTextField iidText = new JTextField(10);
        JButton iidButton = new JButton("确定");

        public void init() {
            listen();

            iidLabel.setFont(new Font("微软雅黑", 1, 16));
            iidText.setFont(new Font("微软雅黑", 1, 14));
            iidButton.setFont(new Font("微软雅黑", 1, 14));

            setLayout(new FlowLayout());
            add(iidLabel);
            add(iidText);
            add(iidButton);

            setVisible(true);
            setSize(500, 100);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        }

        public void listen() {
            String getItem = "select * from items";
            ArrayList<Item> itemList = JDBCUtils.getItemAllInfo(getItem);

            iidButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    boolean povertyIs = false;
                    for (int i = 0; i < itemList.size(); i++) {
                        if (String.valueOf(itemList.get(i).getIid()).equals(iidText.getText())) {
                            povertyIs = true;
                        }
                    }

                    if (povertyIs == false) {
                        new ShowOneItemFailureHint().init();
                    } else {
                        dispose();

                        PublicTableShowInfo dialog = new PublicTableShowInfo();
                        dialog.setSize(600, 150);
                        dialog.init();

                        dtm.getDataVector().removeAllElements();

                        Integer iid = new Integer(iidText.getText());
                        String sql = "select * from items where iid = ?";
                        Item item = JDBCUtils.getItemOneInfo(sql, iid);

                        String[] title = new String[]{"项目编号", "项目名称", "项目类型"};
                        dtm.setColumnIdentifiers(title);

                        dtm.addRow(new Object[]{item.getIid(), item.getIname(), item.getIclass()});
                    }
                }
            });
        }
    }

    class UpdateUserInfoDialog extends JDialog {
        JPanel p = new JPanel();
        JPanel p1 = new JPanel(new GridLayout(11, 1, 20, 20));
        JPanel p2 = new JPanel(new GridLayout(11, 1, 20, 20));
        JPanel p3 = new JPanel();

        JLabel title = new JLabel("修改个人信息");

        JLabel help = new JLabel("帮扶人员编号：");
        JLabel item = new JLabel("项目编号：");
        JLabel name = new JLabel("姓名：");
        JLabel age = new JLabel("年龄：");
        JLabel sex = new JLabel("性别：");
        JLabel nation = new JLabel("民族：");
        JLabel birthplace = new JLabel("籍贯：");
        JLabel reason = new JLabel("贫困原因：");
        JLabel health = new JLabel("身体状况：");
        JLabel educated = new JLabel("受教育程度：");
        JLabel date = new JLabel("登记时间：");

        JTextField helpText = new JTextField(16);
        JTextField itemText = new JTextField(16);
        JTextField nameText = new JTextField(16);
        JTextField ageText = new JTextField(16);
        JTextField sexText = new JTextField(16);
        JTextField nationText = new JTextField(16);
        JTextField birthplaceText = new JTextField(16);
        JTextField reasonText = new JTextField(16);
        JTextField healthText = new JTextField(16);
        JTextField educatedText = new JTextField(16);
        JTextField dateText = new JTextField(16);

        JButton updateButton = new JButton("修改");

        public void init() {
            listen();

            String sql = "select * from user_poor where pid = ?";
            Poverty poverty = JDBCUtils.getPovertyOneInfo(sql, povertyId);

            title.setFont(new Font("楷体", Font.BOLD, 40));

            help.setFont(new Font("微软雅黑", Font.BOLD, 16));
            item.setFont(new Font("微软雅黑", Font.BOLD, 16));
            name.setFont(new Font("微软雅黑", Font.BOLD, 16));
            age.setFont(new Font("微软雅黑", Font.BOLD, 16));
            sex.setFont(new Font("微软雅黑", Font.BOLD, 16));
            nation.setFont(new Font("微软雅黑", Font.BOLD, 16));
            birthplace.setFont(new Font("微软雅黑", Font.BOLD, 16));
            reason.setFont(new Font("微软雅黑", Font.BOLD, 16));
            health.setFont(new Font("微软雅黑", Font.BOLD, 16));
            educated.setFont(new Font("微软雅黑", Font.BOLD, 16));
            date.setFont(new Font("微软雅黑", Font.BOLD, 16));

            helpText.setFont(new Font("微软雅黑", Font.BOLD, 14));
            itemText.setFont(new Font("微软雅黑", Font.BOLD, 14));
            nameText.setFont(new Font("微软雅黑", Font.BOLD, 14));
            ageText.setFont(new Font("微软雅黑", Font.BOLD, 14));
            sexText.setFont(new Font("微软雅黑", Font.BOLD, 14));
            nationText.setFont(new Font("微软雅黑", Font.BOLD, 14));
            birthplaceText.setFont(new Font("微软雅黑", Font.BOLD, 14));
            reasonText.setFont(new Font("微软雅黑", Font.BOLD, 14));
            healthText.setFont(new Font("微软雅黑", Font.BOLD, 14));
            educatedText.setFont(new Font("微软雅黑", Font.BOLD, 14));
            dateText.setFont(new Font("微软雅黑", Font.BOLD, 14));

            updateButton.setFont(new Font("微软雅黑", Font.BOLD, 20));

            helpText.setText(String.valueOf(poverty.getHid()));
            itemText.setText(String.valueOf(poverty.getIid()));
            nameText.setText(poverty.getPname());
            ageText.setText(String.valueOf(poverty.getPage()));
            sexText.setText(poverty.getPsex());
            nationText.setText(poverty.getPnation());
            birthplaceText.setText(poverty.getBirthplace());
            reasonText.setText(poverty.getReason());
            healthText.setText(poverty.getHealth());
            educatedText.setText(poverty.getEducated());
            dateText.setText(poverty.getDate());

            p.add(title);

            p1.add(help);
            p1.add(item);
            p1.add(name);
            p1.add(age);
            p1.add(sex);
            p1.add(nation);
            p1.add(birthplace);
            p1.add(reason);
            p1.add(health);
            p1.add(educated);
            p1.add(date);

            p2.add(helpText);
            p2.add(itemText);
            p2.add(nameText);
            p2.add(ageText);
            p2.add(sexText);
            p2.add(nationText);
            p2.add(birthplaceText);
            p2.add(reasonText);
            p2.add(healthText);
            p2.add(educatedText);
            p2.add(dateText);

            p3.add(updateButton);

            add(p, BorderLayout.NORTH);
            add(p1, BorderLayout.WEST);
            add(p2, BorderLayout.CENTER);
            add(p3, BorderLayout.SOUTH);

            setVisible(true);
            setSize(500, 700);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        }

        public void listen() {
            updateButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String getHelp = "select * from user_helper";
                    ArrayList<Help> helpList = JDBCUtils.getHelpAllInfo(getHelp);

                    String getItem = "select * from items";
                    ArrayList<Item> itemList = JDBCUtils.getItemAllInfo(getItem);

                    Boolean helpIs = false;
                    Boolean itemIs = false;
                    Boolean helpIsNULL = false;
                    Boolean itemIsNULL = false;

                    for (int i = 0; i < helpList.size(); i++) {
                        if (String.valueOf(helpList.get(i).getHid()).equals(helpText.getText())) {
                            helpIs = true;
                        }
                    }

                    for (int i = 0; i < itemList.size(); i++) {
                        if (String.valueOf(itemList.get(i).getIid()).equals(itemText.getText())) {
                            itemIs = true;
                        }
                    }

                    if (!"null".equals(helpText.getText())) {
                        helpIsNULL = true;
                    } else {
                        helpText.setForeground(Color.RED);
                        helpText.setText("帮扶人员编号有误！");
                    }

                    if (!"null".equals(itemText.getText())) {
                        itemIsNULL = true;
                    } else {
                        itemText.setForeground(Color.RED);
                        itemText.setText("贫困项目编号有误！");
                    }


                    if (helpIs == false) {
                        helpText.setForeground(Color.RED);
                        helpText.setText("帮扶人员编号有误！");
                    }

                    if (itemIs == false) {
                        itemText.setForeground(Color.RED);
                        itemText.setText("贫困项目编号有误！");
                    }
                    if (!helpText.getText().equals("") && !itemText.getText().equals("") &&
                            !nameText.getText().equals("") && !ageText.getText().equals("") &&
                            !sexText.getText().equals("") && !nationText.getText().equals("") &&
                            !birthplaceText.getText().equals("") && !reasonText.getText().equals("") &&
                            !healthText.getText().equals("") && !educatedText.getText().equals("") &&
                            !dateText.getText().equals("")) {

                        if (helpIs && itemIs && helpIsNULL && itemIsNULL) {
                            String update = "update user_poor set hid = ?, iid = ?, pname = ?, page = ?, psex = ?, pnation = ?, birthplace = ?, reason = ?, health = ?, educated = ?, date = ? where pid = ?";
                            JDBCUtils.UpdatePovertyInfo(update, helpText.getText(), itemText.getText(), nameText.getText(),
                                    ageText.getText(), sexText.getText(), nationText.getText(), birthplaceText.getText(),
                                    reasonText.getText(), healthText.getText(), educatedText.getText(), dateText.getText(), povertyId);

                            String sql = "select * from user_poor";
                            ArrayList<Poverty> list = JDBCUtils.getPovertyAllInfo(sql);

                            dtmPoverty.getDataVector().removeAllElements();

                            String[] title = new String[]{"贫困人员编号", "帮扶人员编号", "项目编号", "姓名", "年龄", "性别", "民族", "籍贯", "贫困原因", "身体状况", "受教育程度", "登记时间"};
                            dtmPoverty.setColumnIdentifiers(title);

                            for (int i = 0; i < list.size(); i++) {
                                dtmPoverty.addRow(new Object[]{list.get(i).getPid(), list.get(i).getHid(), list.get(i).getIid(), list.get(i).getPname(),
                                        list.get(i).getPage(), list.get(i).getPsex(), list.get(i).getPnation(), list.get(i).getBirthplace(),
                                        list.get(i).getReason(), list.get(i).getHealth(), list.get(i).getEducated(), list.get(i).getDate()});
                            }

                            dispose();
                        }
                    }

                    if (helpText.getText().equals("")) {
                        helpText.setForeground(Color.RED);
                        helpText.setText("此框不能为空！");
                    }

                    if (itemText.getText().equals("")) {
                        itemText.setForeground(Color.RED);
                        itemText.setText("此框不能为空！");
                    }

                    if (nameText.getText().equals("")) {
                        nameText.setForeground(Color.RED);
                        nameText.setText("此框不能为空！");
                    }

                    if (ageText.getText().equals("")) {
                        ageText.setForeground(Color.RED);
                        ageText.setText("此框不能为空！");
                    }

                    if (sexText.getText().equals("")) {
                        sexText.setForeground(Color.RED);
                        sexText.setText("此框不能为空！");
                    }

                    if (nationText.getText().equals("")) {
                        nationText.setForeground(Color.RED);
                        nationText.setText("此框不能为空！");
                    }

                    if (birthplaceText.getText().equals("")) {
                        birthplaceText.setForeground(Color.RED);
                        birthplaceText.setText("此框不能为空！");
                    }

                    if (reasonText.getText().equals("")) {
                        reasonText.setForeground(Color.RED);
                        reasonText.setText("此框不能为空！");
                    }

                    if (healthText.getText().equals("")) {
                        healthText.setForeground(Color.RED);
                        healthText.setText("此框不能为空！");
                    }

                    if (educatedText.getText().equals("")) {
                        educatedText.setForeground(Color.RED);
                        educatedText.setText("此框不能为空！");
                    }

                    if (dateText.getText().equals("")) {
                        dateText.setForeground(Color.RED);
                        dateText.setText("此框不能为空！");
                    }
                }
            });

            helpText.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    helpText.setText("");
                    helpText.setForeground(Color.BLACK);
                }
            });

            itemText.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    itemText.setText("");
                    itemText.setForeground(Color.BLACK);
                }
            });

            nameText.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    nameText.setText("");
                    nameText.setForeground(Color.BLACK);
                }
            });

            ageText.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    ageText.setText("");
                    ageText.setForeground(Color.BLACK);
                }
            });

            sexText.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    sexText.setText("");
                    sexText.setForeground(Color.BLACK);
                }
            });

            nationText.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    nationText.setText("");
                    nationText.setForeground(Color.BLACK);
                }
            });

            birthplaceText.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    birthplaceText.setText("");
                    birthplaceText.setForeground(Color.BLACK);
                }
            });

            reasonText.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    reasonText.setText("");
                    reasonText.setForeground(Color.BLACK);
                }
            });

            healthText.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    healthText.setText("");
                    healthText.setForeground(Color.BLACK);
                }
            });

            educatedText.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    educatedText.setText("");
                    educatedText.setForeground(Color.BLACK);
                }
            });

            dateText.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    dateText.setText("");
                    dateText.setForeground(Color.BLACK);
                }
            });
        }
    }

    class PovertyJoinItem extends JDialog {
        JPanel p = new JPanel();
        JPanel p1 = new JPanel(new GridLayout(11, 1, 25, 25));
        JPanel p2 = new JPanel(new GridLayout(11, 1, 25, 25));
        JPanel p3 = new JPanel();

        JLabel title = new JLabel("登记信息");

        JLabel help = new JLabel("帮扶人员编号：");
        JLabel item = new JLabel("项目编号：");
        JLabel name = new JLabel("姓名：");
        JLabel age = new JLabel("年龄：");
        JLabel sex = new JLabel("性别：");
        JLabel nation = new JLabel("民族：");
        JLabel birthplace = new JLabel("籍贯：");
        JLabel reason = new JLabel("贫困原因：");
        JLabel health = new JLabel("身体状况：");
        JLabel educated = new JLabel("受教育程度：");
        JLabel date = new JLabel("登记时间：");

        JTextField helpText = new JTextField(16);
        JTextField itemText = new JTextField(16);
        JTextField nameText = new JTextField(16);
        JTextField ageText = new JTextField(16);
        JTextField sexText = new JTextField(16);
        JTextField nationText = new JTextField(16);
        JTextField birthplaceText = new JTextField(16);
        JTextField reasonText = new JTextField(16);
        JTextField healthText = new JTextField(16);
        JTextField educatedText = new JTextField(16);
        JTextField dateText = new JTextField(16);

        JButton addItemButton = new JButton("登记");

        public void init() {
            listen();
            title.setFont(new Font("楷体", Font.BOLD, 40));

            help.setFont(new Font("微软雅黑", Font.BOLD, 16));
            item.setFont(new Font("微软雅黑", Font.BOLD, 16));
            name.setFont(new Font("微软雅黑", Font.BOLD, 16));
            age.setFont(new Font("微软雅黑", Font.BOLD, 16));
            sex.setFont(new Font("微软雅黑", Font.BOLD, 16));
            nation.setFont(new Font("微软雅黑", Font.BOLD, 16));
            birthplace.setFont(new Font("微软雅黑", Font.BOLD, 16));
            reason.setFont(new Font("微软雅黑", Font.BOLD, 16));
            health.setFont(new Font("微软雅黑", Font.BOLD, 16));
            educated.setFont(new Font("微软雅黑", Font.BOLD, 16));
            date.setFont(new Font("微软雅黑", Font.BOLD, 16));

            helpText.setFont(new Font("微软雅黑", Font.BOLD, 14));
            itemText.setFont(new Font("微软雅黑", Font.BOLD, 14));
            nameText.setFont(new Font("微软雅黑", Font.BOLD, 14));
            ageText.setFont(new Font("微软雅黑", Font.BOLD, 14));
            sexText.setFont(new Font("微软雅黑", Font.BOLD, 14));
            nationText.setFont(new Font("微软雅黑", Font.BOLD, 14));
            birthplaceText.setFont(new Font("微软雅黑", Font.BOLD, 14));
            reasonText.setFont(new Font("微软雅黑", Font.BOLD, 14));
            healthText.setFont(new Font("微软雅黑", Font.BOLD, 14));
            educatedText.setFont(new Font("微软雅黑", Font.BOLD, 14));
            dateText.setFont(new Font("微软雅黑", Font.BOLD, 14));

            addItemButton.setFont(new Font("微软雅黑", Font.BOLD, 20));

            dateText.setText("格式为：YYYY-MM-DD");

            p.add(title);

            p1.add(help);
            p1.add(item);
            p1.add(name);
            p1.add(age);
            p1.add(sex);
            p1.add(nation);
            p1.add(birthplace);
            p1.add(reason);
            p1.add(health);
            p1.add(educated);
            p1.add(date);

            p2.add(helpText);
            p2.add(itemText);
            p2.add(nameText);
            p2.add(ageText);
            p2.add(sexText);
            p2.add(nationText);
            p2.add(birthplaceText);
            p2.add(reasonText);
            p2.add(healthText);
            p2.add(educatedText);
            p2.add(dateText);

            p3.add(addItemButton);

            add(p, BorderLayout.NORTH);
            add(p1, BorderLayout.WEST);
            add(p2, BorderLayout.CENTER);
            add(p3, BorderLayout.SOUTH);

            setVisible(true);
            setSize(450, 700);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        }

        public void listen() {
            addItemButton.addActionListener(new ActionListener() {
                @Override
                public void actionPerformed(ActionEvent e) {
                    String getHelp = "select * from user_helper";
                    ArrayList<Help> helpList = JDBCUtils.getHelpAllInfo(getHelp);

                    String getItem = "select * from items";
                    ArrayList<Item> itemList = JDBCUtils.getItemAllInfo(getItem);

                    Boolean helpIs = false;
                    Boolean itemIs = false;

                    if (!helpText.getText().equals("") && !itemText.getText().equals("") &&
                            !nameText.getText().equals("") && !ageText.getText().equals("") &&
                            !sexText.getText().equals("") && !nationText.getText().equals("") &&
                            !birthplaceText.getText().equals("") && !reasonText.getText().equals("") &&
                            !healthText.getText().equals("") && !educatedText.getText().equals("") &&
                            !dateText.getText().equals("") && !dateText.getText().equals("格式为：YYYY-MM-DD")) {

                        for (int i = 0; i < helpList.size(); i++) {
                            if (String.valueOf(helpList.get(i).getHid()).equals(helpText.getText())) {
                                helpIs = true;
                            }
                        }

                        for (int i = 0; i < itemList.size(); i++) {
                            if (String.valueOf(itemList.get(i).getIid()).equals(itemText.getText())) {
                                itemIs = true;
                            }
                        }

                        if (helpIs == false) {
                            helpText.setForeground(Color.RED);
                            helpText.setText("帮扶人员编号有误！");
                        }

                        if (itemIs == false) {
                            itemText.setForeground(Color.RED);
                            itemText.setText("贫困项目编号有误！");
                        }

                        if (helpIs && itemIs) {
                            String insert = "insert into user_poor(hid, iid, pname, page, psex, pnation, birthplace, reason, health, educated, date) " +
                                    "values(?, ?, ?, ?, ?, ?, ?, ?, ?, ?, ?)";
                            JDBCUtils.addPoverty(insert, helpText.getText(), itemText.getText(), nameText.getText(),
                                    ageText.getText(), sexText.getText(), nationText.getText(), birthplaceText.getText(),
                                    reasonText.getText(), healthText.getText(), educatedText.getText(), dateText.getText());

                            String select = "select * from user_poor where hid = ? and iid = ? and pname = ?";
                            Poverty poverty = JDBCUtils.getPovertyOneInfo(select, helpText.getText(), itemText.getText(), nameText.getText());
                            povertyId = poverty.getPid();

                            dtmPoverty.getDataVector().removeAllElements();

                            String sql = "select * from user_poor";
                            ArrayList<Poverty> list = JDBCUtils.getPovertyAllInfo(sql);

                            String[] title = new String[]{"贫困人员编号", "帮扶人员编号", "项目编号", "姓名", "年龄", "性别", "民族", "籍贯", "贫困原因", "身体状况", "受教育程度", "登记时间"};
                            dtmPoverty.setColumnIdentifiers(title);

                            for (int i = 0; i < list.size(); i++) {
                                dtmPoverty.addRow(new Object[]{list.get(i).getPid(), list.get(i).getHid(), list.get(i).getIid(), list.get(i).getPname(),
                                        list.get(i).getPage(), list.get(i).getPsex(), list.get(i).getPnation(), list.get(i).getBirthplace(),
                                        list.get(i).getReason(), list.get(i).getHealth(), list.get(i).getEducated(), list.get(i).getDate()});
                            }

                            userRegisterInfo.setVisible(false);
                            bindUser.setVisible(false);
                            logoutInfo.setVisible(true);
                            lookMe.setVisible(true);
                            showMyHelp.setVisible(true);
                            update.setVisible(true);

                            dispose();
                        }
                    }

                    if (helpText.getText().equals("")) {
                        helpText.setForeground(Color.RED);
                        helpText.setText("此框不能为空！");
                    }

                    if (itemText.getText().equals("")) {
                        itemText.setForeground(Color.RED);
                        itemText.setText("此框不能为空！");
                    }

                    if (nameText.getText().equals("")) {
                        nameText.setForeground(Color.RED);
                        nameText.setText("此框不能为空！");
                    }

                    if (ageText.getText().equals("")) {
                        ageText.setForeground(Color.RED);
                        ageText.setText("此框不能为空！");
                    }

                    if (sexText.getText().equals("")) {
                        sexText.setForeground(Color.RED);
                        sexText.setText("此框不能为空！");
                    }

                    if (nationText.getText().equals("")) {
                        nationText.setForeground(Color.RED);
                        nationText.setText("此框不能为空！");
                    }

                    if (birthplaceText.getText().equals("")) {
                        birthplaceText.setForeground(Color.RED);
                        birthplaceText.setText("此框不能为空！");
                    }

                    if (reasonText.getText().equals("")) {
                        reasonText.setForeground(Color.RED);
                        reasonText.setText("此框不能为空！");
                    }

                    if (healthText.getText().equals("")) {
                        healthText.setForeground(Color.RED);
                        healthText.setText("此框不能为空！");
                    }

                    if (educatedText.getText().equals("")) {
                        educatedText.setForeground(Color.RED);
                        educatedText.setText("此框不能为空！");
                    }

                    if (dateText.getText().equals("")) {
                        dateText.setForeground(Color.RED);
                        dateText.setText("此框不能为空！");
                    }

                }
            });

            helpText.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    helpText.setText("");
                    helpText.setForeground(Color.BLACK);
                }
            });

            itemText.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    itemText.setText("");
                    itemText.setForeground(Color.BLACK);
                }
            });

            nameText.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    nameText.setText("");
                    nameText.setForeground(Color.BLACK);
                }
            });

            ageText.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    ageText.setText("");
                    ageText.setForeground(Color.BLACK);
                }
            });

            sexText.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    sexText.setText("");
                    sexText.setForeground(Color.BLACK);
                }
            });

            nationText.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    nationText.setText("");
                    nationText.setForeground(Color.BLACK);
                }
            });

            birthplaceText.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    birthplaceText.setText("");
                    birthplaceText.setForeground(Color.BLACK);
                }
            });

            reasonText.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    reasonText.setText("");
                    reasonText.setForeground(Color.BLACK);
                }
            });

            healthText.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    healthText.setText("");
                    healthText.setForeground(Color.BLACK);
                }
            });

            educatedText.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    educatedText.setText("");
                    educatedText.setForeground(Color.BLACK);
                }
            });

            dateText.addMouseListener(new MouseAdapter() {
                @Override
                public void mouseClicked(MouseEvent e) {
                    dateText.setText("");
                    dateText.setForeground(Color.BLACK);
                }
            });
        }
    }

    class PublicTableShowInfo extends JDialog {
        public void init() {
            listen();

            head.setFont(new Font("微软雅黑", 1, 15));
            head.setReorderingAllowed(false);

            table.setFont(new Font("微软雅黑", 1, 13));
            table.setEnabled(false);
            table.setRowHeight(20);

            add(scroll);

            setVisible(true);
            setLocationRelativeTo(null);
            setDefaultCloseOperation(JFrame.DISPOSE_ON_CLOSE);
        }

        public void listen() {
            addWindowListener(new WindowAdapter() {
                @Override
                public void windowClosing(WindowEvent e) {
                    dispose();
                }
            });
        }
    }
}
